package com.nyp.lotteryf.view;

import java.io.Serializable;

import javax.ejb.DuplicateKeyException;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.richfaces.application.push.MessageException;
import org.richfaces.application.push.TopicKey;
import org.richfaces.application.push.TopicsContext;

import com.nyp.lotteryf.entity.User;

@Named
@SessionScoped
public class SubmitNameBean implements Serializable{
	
	@Inject NameCollectionAppBean nameCollection;
	
	private User user = new User();
	
	public String submitEmailAddress(){
		try {
			nameCollection.addUser(user.clone());
			push();
		} 
		catch (DuplicateKeyException e) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, 
					"Ez az e-mail cím egyszer már be lett küldve.", 
					""));
			return null;
		} 
		catch (MessageException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "Thanks";
	}
	
	private void push() throws MessageException {
	    TopicKey topicKey = new TopicKey("topic1");
	    TopicsContext topicsContext = TopicsContext.lookup();
	  
	    topicsContext.publish(topicKey, "empty message");
	  
	    System.out.println("push event");
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}
